﻿using System;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms.Design;
using System.Drawing.Design;

namespace MyFastDataSourceBind
{
    /// <summary>
    /// 用于编辑系统代码类别的编辑器
    /// </summary>
    /// <remarks>编制 袁永福</remarks>
    public class XSystemDictionaryNameEditor : UITypeEditor
    {
        /// <summary>
        /// 初始化对象
        /// </summary>
        public XSystemDictionaryNameEditor()
        {
        }
        /// <summary>
        /// 返回编辑类型为下拉列表类型
        /// </summary>
        /// <param name="context">参数</param>
        /// <returns>下拉列表类型</returns>
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            return UITypeEditorEditStyle.DropDown;
        }
        /// <summary>
        /// 编辑数值
        /// </summary>
        /// <param name="context">参数</param>
        /// <param name="provider">参数</param>
        /// <param name="Value">旧数据</param>
        /// <returns>编辑后的新的数据</returns>
        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object Value)
        {
            if (provider == null)
                return Value;
            // 获得服务对象
            IWindowsFormsEditorService svc =
                (IWindowsFormsEditorService)provider.GetService(
                typeof(IWindowsFormsEditorService));
            if (svc == null)
                return Value;
            // 获得所有的系统字典类别名称数组
            string[] names = XSystemDictionary.GetCategoryNames();
            if (names == null || names.Length == 0)
                return Value;
            using (DesignPopupListBox lst = new DesignPopupListBox())
            {
                // 向列表填充系统字典类别名称
                foreach (string name in names)
                {
                    lst.AddItem(name, null);
                }
                lst.Height = 300;
                string v = lst.Popup(svc, Convert.ToString(Value));
                if (v != null)
                {
                    // 返回用户选择的值
                    return v;
                }
            }
            return Value;
        }//public override object EditValue()
    }//public class XSystemCodeUITypeEditor : UITypeEditor
}